package org.practice.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.practice.dao.BookDao;
import org.practice.dao.CategoryDao;
import org.practice.dao.DaoFactory;
import org.practice.form.BookListForm;
import org.practice.model.Book;
import org.practice.model.Category;

public class BookListAction extends Action{
	public static final int ROWS_PER_PAGE=5;
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BookListForm bf=(BookListForm)form;
		Integer categoryId=bf.getThreeCatId();
		Integer page=bf.getPage();
		Integer orderBy=bf.getOrderBy();
		BookDao dao=DaoFactory.createBookDao();
		List<Book> allBooks=dao.findBookByCategoryId(categoryId, page, ROWS_PER_PAGE, orderBy);
		
		Map<Integer,String> orderOptions=dao.getOrderOptions();
		Integer totalPages=dao.getTotalPagesByCategoryId(categoryId, ROWS_PER_PAGE);
		CategoryDao catDao=DaoFactory.createCategoryDao();
		Category category=catDao.findCategoryById(bf.getTwoCatId());
		
		request.setAttribute("orderOptions", orderOptions);
		request.setAttribute("bookList", allBooks);
		request.setAttribute("cat2", category);
		request.setAttribute("totalPages", totalPages);
		
		return mapping.findForward("success");
	}
	

}
